package com.ego.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.pojo.EasyUIDatagrid;

import com.ego.commons.pojo.EgoResult;
import com.ego.commons.utils.IDUtils;
import com.ego.dubbo.service.TbContentDubboService;
import com.ego.pojo.TbContent;
import com.ego.sender.Send;
import com.ego.service.TbContentService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author GHP
 * 分页查询实现
 */
@Service
public class TbContentServiceImpl implements TbContentService {

    @Reference
    private TbContentDubboService tbContentDubboService;

    @Autowired
    private Send send;

    /**
     * 获取队列名称
     */
    @Value("content")
    private String contentQueue;

    @Value("89")
    private Long bigadId;

    /**
     * 分页查询
     * @author GHP
     * @param categoryId
     * @param page
     * @param rows
     * @return
     */
    @Override
    public EasyUIDatagrid showContent(Long categoryId, int page, int rows) {
        List<TbContent> list = this.tbContentDubboService.selectByPage(categoryId, page, rows);
        long total = tbContentDubboService.selectCountByCategoryId(categoryId);
        return new EasyUIDatagrid(list,total);
    }

    @Override
    public EgoResult insert(TbContent tbContent) {
        tbContent.setId(IDUtils.genItemId());
        Date date = new Date();
        tbContent.setUpdated(date);
        tbContent.setCreated(date);
        try {
            int index = this.tbContentDubboService.insert(tbContent);
            if (index ==1){
                //数据库新增，同步缓存
                //向rabbitmq发送消息，实现异步缓存，由于是异步缓存，对性能没有损耗。
                if(tbContent.getCategoryId().equals(bigadId)){
                    send.send(contentQueue,"async");
                }
                System.out.println("发送了消息");
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return EgoResult.error("新增失败");
    }

    @Override
    public EgoResult update(TbContent tbContent) {
        try {
            tbContent.setUpdated(new Date());
                int index = this.tbContentDubboService.update(tbContent);
            if (index ==1){
                if(tbContent.getCategoryId().equals(bigadId)){
                    send.send(contentQueue,"async");
                }
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return EgoResult.error("修改失败");
    }

    @Override
    public EgoResult deleteByIds(long[] ids) {
        try {
            int index = this.tbContentDubboService.deleteByIds(ids);
            if (index ==1){
                send.send(contentQueue,"async");
                return EgoResult.ok();
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return EgoResult.error("删除失败");
    }
}
